package com.whitedust.top90;

import com.whitedust.structure.TreeNode;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 *
 * @author A
 * @since 2025/11/16 21:04
 */

public class Top85 {
    int result = 0;
    Map<Integer, Integer> minValue = new HashMap();

    public int widthOfBinaryTree(TreeNode root) {
        depth(root, 1, 1);
        return result;
    }

    //nodeIndex为当前节点的索引，level为深度
    public void depth(TreeNode node, int level, int nodeIndex) {
        if (node == null) return;
        minValue.putIfAbsent(level, nodeIndex);
        result = Math.max(result, nodeIndex - minValue.get(level) + 1);
        depth(node.left, level + 1, 2 * nodeIndex);
        depth(node.right, level + 1, 2 * nodeIndex + 1);
    }
}
